package com.juku2024.juku.tools

object VideoAnalysisUtils {

    /** 是否使用解析 */
    fun isUseParse(user: Boolean){
        AnalysisUtils.analysis(Constants.EVENT_PLAY, Constants.PARSE_SELF, "userParse: $user")
    }

    /** 机械统计: 对解析做概率统计
     *      解析时间
     *      解析结果
     *      解析时间+结果
     */
    fun parseTimeAndResult(sourceName: String?, time: Long, result: Boolean){
        val finalSourceName = sourceName?:"unknown"
        val timeSecond = time / 1000
        AnalysisUtils.analysis(Constants.EVENT_PLAY, Constants.PARSE_SOURCE_TIME, "$finalSourceName: $timeSecond")
        AnalysisUtils.analysis(Constants.EVENT_PLAY, Constants.PARSE_SOURCE_RESULT, "$finalSourceName: $result")
        AnalysisUtils.analysis(Constants.EVENT_PLAY, Constants.PARSE_SOURCE_TIME_RESULT, "$finalSourceName *|* $timeSecond  *|* $result")
    }

    /**
     * 统计失败: 方便定位哪个视频，哪个源，哪一集出错
     *     videoName + sourceName + title
     */
    fun parseError(videoName: String?, sourceName: String?, title: String?){
        val finalSourceName = sourceName?:"unknown"
        AnalysisUtils.analysis(Constants.EVENT_PLAY, Constants.PARSE_NAME_SOURCE_EPISODE_FAIL, "$videoName *|* $finalSourceName *|* $title")
    }


    /** 统计自动切源 */
    fun parseFinalSource(firstSource: Int, finalSource: Int){
        AnalysisUtils.analysis(Constants.EVENT_PLAY, Constants.FINAL_PLAY_SOURCE, "${getSourceName(firstSource)} *|* ${getSourceName(finalSource)}")
    }

    /** 统计播放源的首次缓冲时间，只统计成功的 */
    fun parseFirstBufferTime(sourceId: Int, time: Long){
        AnalysisUtils.analysis(Constants.EVENT_PLAY, Constants.PLAY_SOURCE_FIRST_BUFFER_TIME, "${getSourceName(sourceId)} *|* ${time / 1000}")
    }


    /** 统计播放源的缓冲次数 */
    fun parseBufferTimes(sourceId: Int, time: String){
        AnalysisUtils.analysis(Constants.EVENT_PLAY, Constants.PLAY_SOURCE_BUFFER_TIMES, "${getSourceName(sourceId)} *|* $time")
    }

    /** 统计播放播放失败的原因 */
    fun sourcePlayError(videoName: String?, sourceId: Int, episode: Int, error: Int){
        AnalysisUtils.analysis(Constants.EVENT_PLAY, Constants.PLAY_SOURCE_ERROR, "${getSourceName(sourceId)} *|* $error")

        AnalysisUtils.analysis(Constants.EVENT_PLAY, Constants.PLAY_NAME_SOURCE_ERROR, "$videoName *|* ${getSourceName(sourceId)} *|* $episode *|* $error")
    }

    /** 统计自动切换播放器的源 */
    fun autoChangePlayerBySource(sourceId: Int){
        AnalysisUtils.analysis(Constants.EVENT_PLAY, Constants.PLAY_SOURCE_AUTO_CHANGE_DECODER, getSourceName(sourceId))
    }

    /** 统计切换备用解析源的次数 */
    fun autoChangeParseSource(){
        AnalysisUtils.analysis(Constants.EVENT_PLAY, Constants.PLAY_ERROR_OTHER_SOURCE, 1)
    }


    /** 统计播放的时候，某个源自动切换了几次 */
    fun playAutoChangeSourceTimes(sourceId: Int){
        AnalysisUtils.analysis(Constants.EVENT_CHANGE_SOURCE, getSourceName(sourceId), 1)
    }

    /** 统计出错的时候，是否已经播放一段时间 */
    fun playErrorAfterReady(videoName: String?, sourceId: Int?, episode: Int, error: Int){
        AnalysisUtils.analysis(Constants.EVENT_PLAY, Constants.PLAY_ERROR_IS_AFTER_READY, "$videoName *|* ${getSourceName(sourceId)} *|* $episode *|* $error")
    }

    /** 统计出错的时候，是否已经播放一段时间 */
    fun playVideoDuration(sourceId: Int?, time: Long){
        AnalysisUtils.analysis(Constants.EVENT_PLAY, Constants.PLAY_SOURCE_TIME, "${getSourceName(sourceId)} *|* ${getPlayTime(time)}")
    }

    private fun getPlayTime(time: Long): String{
        return if (time <= 1000 * 60) {
            "0-1分钟"
        } else if (time <= 5 * 60 * 1000) {
            "1-5分钟"
        } else if (time <= 10 * 60 * 1000) {
            "5-10分钟"
        } else {
            "10分钟以上"
        }
    }

    private fun getSourceName(sourceId: Int?): String{
        return when (sourceId){
            1 -> "北斗星"
            2 -> "无尽"
            3 -> "快播"
            4 -> "红牛"
            5 -> "淘片"
            6 -> "天空"
            7 -> "想看"
            8 -> "百度云"
            9 -> "飞速"
            10 -> "闪电"
            11 -> "酷点"
            12 -> "U酷"
            13 -> "鱼乐"
            14 -> "youtube"
            15 -> "八戒"
            16 -> "速更"
            17 -> "if101"
            18 -> "光速"
            19 -> "卧龙"
            20 -> "乐多（需解析）"
            21 -> "腾讯"
            22 -> "爱奇艺"
            23 -> "优酷"
            24 -> "芒果"
            25 -> "乐视"
            26 -> "搜狐"
            27 -> "PPTV"
            28 -> "bili"
            29 -> "OK"
            30 -> "神速"
            31 -> "新浪"
            32 -> "松鼠"
            33 -> "fox"
            34 -> "快车"
            35 -> "tom"
            36 -> "金鹰"
            37 -> "量子"
            else -> "unknown"
        }
    }
}